package com.zany.children.service;

import com.zany.children.dao.ChargesOwedDao;
import com.zany.children.dao.ChargesOwedDetailDao;
import com.zany.children.vo.ChargesOwed;
import com.zany.children.vo.ChargesOwedDetail;
import com.zany.children.vo.PaymentProject;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 欠费记录service.
 */
public class ChargesOwedServiceImpl implements ChargesOwedService {

    public ChargesOwedDao getChargesOwedDao() {
        return chargesOwedDao;
    }

    public void setChargesOwedDao(ChargesOwedDao chargesOwedDao) {
        this.chargesOwedDao = chargesOwedDao;
    }

    private ChargesOwedDao chargesOwedDao;

    private ChargesOwedDetailDao chargesOwedDetailDao;

    public ChargesOwedDetailDao getChargesOwedDetailDao() {
        return chargesOwedDetailDao;
    }

    public void setChargesOwedDetailDao(ChargesOwedDetailDao chargesOwedDetailDao) {
        this.chargesOwedDetailDao = chargesOwedDetailDao;
    }

    @Override
    public Long saveChargesOwed(ChargesOwed chargesOwed) {
        return chargesOwedDao.saveChargesOwed(chargesOwed);
    }

    @Override
    public void updateChargesOwed(ChargesOwed chargesOwed) {

        chargesOwedDao.updateChargesOwed(chargesOwed);

    }

    @Override
    public void deleteChargesOwedById(Long id) {
        chargesOwedDao.deleteChargesOwedById(id);
    }

    @Override
    public List<ChargesOwed> getChargesOwedListPage(ChargesOwed chargesOwed) {
        List<ChargesOwed> page = chargesOwedDao.getChargesOwedListPage(chargesOwed);
        return page;
    }

    @Override
    public Integer getChargesOwedListPageCount(ChargesOwed chargesOwed) {
        Integer count = chargesOwedDao.getChargesOwedListPageCount(chargesOwed);
        return count;
    }

    @Override
    public ChargesOwed getChargesOwedById(Long id) {

        return chargesOwedDao.getChargesOwedById(id);
    }

    @Override
    public void saveChargesOwedAndDetail(List<ChargesOwed> chargesOweds) {
        for(ChargesOwed chargesOwed:chargesOweds) {
            ChargesOwed chargesOwed1 = new ChargesOwed();
            chargesOwed1.setPageStart(1);
            chargesOwed1.setPageEnd(1000);
            chargesOwed1.setClassId(chargesOwed.getClassId());
            chargesOwed1.setStuId(chargesOwed.getStuId());
            List<ChargesOwed> chargesOwedListPage = chargesOwedDao.getChargesOwedListPage(chargesOwed1);
            Long chargesOwedId = null;
            if(chargesOwedListPage!=null&&chargesOwedListPage.size()>0){
                chargesOwedId = chargesOwedListPage.get(0).getId();
            }else{
                chargesOwedId = chargesOwedDao.saveChargesOwed(chargesOwed);
            }
            List<ChargesOwedDetail> owedDetailList = chargesOwed.getChargesOwedDetailList();
            for (ChargesOwedDetail chargesOwedDetail : owedDetailList) {
                chargesOwedDetail.setOwedId(chargesOwedId);
                ChargesOwedDetail queryParam = new ChargesOwedDetail();
                queryParam.setClassId(chargesOwedDetail.getClassId());
                queryParam.setProjectId(chargesOwedDetail.getProjectId());
                queryParam.setStuId(chargesOwedDetail.getStuId());
                Integer count = chargesOwedDetailDao.getChargesOwedDetailListPageCount(queryParam);
                if(count==0) {
                    chargesOwedDetailDao.saveChargesOwedDetail(chargesOwedDetail);
                }
            }
            Map<String,Object> param = new HashMap<String,Object>();
            param.put("stuId",chargesOwed.getStuId());
            param.put("classId",chargesOwed.getClassId());
            chargesOwedDao.updateChargesOwedMoney(param);
        }
    }

    @Override
    public List<PaymentProject> getOwedPaymentProjectByClassId(Long classId) {
        return chargesOwedDao.getOwedPaymentProjectByClassId(classId);
    }
}
